package libraryRun;

import mySQLServer.Link;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Register extends JFrame {
    public Register(){
        super("注册界面");
    }

    public void registerMethod(Register register,MainFrame myJFrame) {
        setLayout(null);
        Container container=this.getContentPane();
        setSize(1200,650);

        //面板
        JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER,0,35));
        JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER,20,40));

        //标签
        JLabel label1=new JLabel("用 户 注 册");
        JLabel label2=new JLabel("用 户 名");
        JLabel label3=new JLabel("用 户 Id");
        JLabel label4=new JLabel("密 码");
        JLabel label5=new JLabel("确 认 密 码");

        //文本行
        JTextField field1=new JTextField(15);
        JTextField field2=new JTextField(15);

        //密码框
        JPasswordField password1=new JPasswordField(15);
        JPasswordField password2=new JPasswordField(15);

        //按钮
        JButton button1=new JButton("注 册");
        JButton button2=new JButton("取 消");

        //监听器
        ImageIcon imageIcon1=new ImageIcon("icon/imageIcon6.jpg");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(50,45,Image.SCALE_AREA_AVERAGING));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    if(JOptionPane.showConfirmDialog(register,"你确定吗？","注册提示",JOptionPane.YES_NO_CANCEL_OPTION,1,imageIcon1)==0){
                        String s=new String(password1.getPassword());
                        String s1=new String(password2.getPassword());
                        if(field1.getText().equals("")||field2.getText().equals("")||s.equals("")||s1.equals("")){
                            JOptionPane.showMessageDialog(register,"请完成注册！","注册提示",1,imageIcon1);
                            return;
                        }
                        if(field2.getText().length()!=11){
                            JOptionPane.showMessageDialog(register,"用户Id必须是十一位！","注册提示",1,imageIcon1);
                            return;
                        }
                            if(s.equals(s1)){
                                Link link=new Link();
                                try{
                                    Connection con=link.linkDatebase();
                                    insertUser(con,field1.getText(),field2.getText(),s);
                                    link.closeDatebase(con);
                                    myJFrame.setVisible(true);
                                    dispose();

                                }catch (ClassNotFoundException e1){
                                    e1.printStackTrace();
                                }catch (SQLException e1){
                                    e1.printStackTrace();
                                }
                            }
                            else{
                                JOptionPane.showMessageDialog(register,"密码不一样","注册提示",1,imageIcon1);
                            }
                    }
            }
        });

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myJFrame.setVisible(true);
                dispose();
            }
        });

        addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {

            }

            public void windowClosing(WindowEvent e) {
                myJFrame.setVisible(true);
            }

            public void windowClosed(WindowEvent e) {

            }

            public void windowIconified(WindowEvent e) {

            }

            public void windowDeiconified(WindowEvent e) {

            }

            public void windowActivated(WindowEvent e) {

            }


            public void windowDeactivated(WindowEvent e) {

            }
        });

        //字体
        Font font=new Font("宋体",Font.BOLD,45);
        Font font1=new Font("宋体",Font.BOLD,30);
        Font font2=new Font("宋体",Font.BOLD,20);


        //设置字体
        label1.setFont(font);
        label2.setFont(font1);
        label3.setFont(font1);
        label4.setFont(font1);
        label5.setFont(font1);
        field1.setFont(font2);
        field2.setFont(font2);
        password1.setFont(font2);
        password2.setFont(font2);

        //设置尺寸
        label2.setPreferredSize(new Dimension(180,30));
        label3.setPreferredSize(new Dimension(180,30));
        label4.setPreferredSize(new Dimension(180,30));
        label5.setPreferredSize(new Dimension(180,30));
        button1.setPreferredSize(new Dimension(100,30));
        button2.setPreferredSize(new Dimension(100,30));

        //设置位置
        p1.setBounds(250,30,700,120);
        p2.setBounds(340,170,500,350);

        //放入面板
        p1.add(label1);

        p2.add(label2);
        p2.add(field1);
        p2.add(label3);
        p2.add(field2);
        p2.add(label4);
        p2.add(password1);
        p2.add(label5);
        p2.add(password2);
        p2.add(button1);
        p2.add(button2);


        //放入容器
        container.add(p1);
        container.add(p2);

        setIconImage((new ImageIcon("icon/imageIcon5.jpg")).getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
    public void insertUser(Connection con, String userName, String userId, String password) throws SQLException{
        Statement stmt=con.createStatement();
        String sql="insert into [user](userName,userId,userPassword) values('"+userName+"','"+userId+"','"+password+"')";
        stmt.executeUpdate(sql);
    }


}
